#pragma once

#include<vector>

namespace lcy
{
	template<class T, class Container = vector<T>, class Compare = Less<T>>
	class priority_queue
	{
	public:
		priority_queue() 
		{}

		template <class InputIterator>
		priority_queue(InputIterator first, InputIterator last)
		{
			_con(first, last);
			for (size_t i = (size() - 2) / 2; i >= 0; --i)
			{
				adjust_down(i);
			}
		}

		bool empty() const
		{
			return _con.empty();
		}

		size_t size() const
		{
			return _con.size();
		}

		const T& top() const
		{
			return _con[0];
		}

		void adjust_up(size_t child)
		{
			size_t father = (child - 1) / 2;
			while (child > 0)
			{
				if (comp(_con[father], _con[child]))
				{
					swap(_con[father], _con[child]);
					child = father;
					father = (child - 1) / 2;
				}
			}
		}

		void push(const T& x)
		{
			_con.push_back(x);
			adjust_up(size() - 1);
		}

		void adjust_down(size_t father)
		{
			size_t child = father * 2 + 1;
			while (child < size())
			{
				if (child + 1 < size() && comp(_con[child], _con[child + 1]))
				{
					++child;
				}
				if (comp(_con[father], _con[child]))
				{
					swap(_con[father], _con[child]);
					father = child;
					child = father * 2 + 1;
				}
				else
				{
					break;
				}
			}
		}

		void pop()
		{
			swap(_con[0], _con[size() - 1]);
			_con.pop_back();
			adjust_down(0);
		}

	private:
		Container _con;
		Compare comp;
	};

}